/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_httpd.htm
 *
 * Zhiqim Httpd is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.httpd.util;

import java.util.Map.Entry;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.ZmlContextConstants;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.model.maps.MapSS;
import org.zhiqim.kernel.util.Arrays;
import org.zhiqim.kernel.util.Urls;

/**
 * 对HttpRequest进行一级封装，实现相关的数据提取工具类，分隔HttpRequest代码
 * 
 * @version v1.0.0 @author zouzhigang 2014-3-21 新建与整理
 */
@AnAlias("Requests")
public class Requests implements ZmlContextConstants
{
    /**
     * 提取所有参数及其值,并以隐藏控件的方式打出
     */
    public static String printAllParameterInHidden(HttpRequest request)
    {
        StringBuilder strb = new StringBuilder();

        MapSS paramMap = request.getParameterMap();
        for (Entry<String, String> entry : paramMap.entrySet())
        {
            String name = entry.getKey();
            String[] values = Arrays.toStringArray(entry.getValue());
            for (String value : values)
            {//字符串转化为数组
                strb.append("<input type=\"hidden\"").append(" name=\"").append(name).append("\"").append(" value=\"").append(value).append("\">\n");
            }
        }

        return strb.toString();
    }

    /**
     * 提取所有参数,并以URL的方式出现
     */
    public static String getParameterForURL(HttpRequest request)
    {
        if (request.getParameterMap().isEmpty())
            return "";
        
        StringBuilder strb = new StringBuilder("?");
        for (Entry<String, String> entry : request.getParameterMap().entrySet())
        {
            strb.append(entry.getKey()).append("=").append(Urls.encodeUTF8(entry.getValue())).append("&");
        }
        strb.setLength(strb.length()-1);
        return strb.toString();
    }
}